<template>
    <div>
        <el-page-header content="添加产品" icon="" title="产品管理"></el-page-header>
        <el-form
        :model="productForm"
        :rules="productFormRules"
        ref="productFormRef"
        label-width="115px"
        class="demo-ruleForm"
        >
            <el-form-item label="产品名称" prop="title">
                <el-input v-model="productForm.title" placeholder="请输入产品名称"></el-input>
            </el-form-item>
            <el-form-item label="设计工程师" prop="author">
                <el-input v-model="productForm.author" placeholder="请输入设计工程师"></el-input>
            </el-form-item>
            <el-form-item label="产品简要描述" prop="introduction">
                <el-input v-model="productForm.introduction" type="textarea" :rows="2" placeholder="请输入产品简要描述"></el-input>
            </el-form-item>
            <el-form-item label="产品详细描述" prop="detail">
                <el-input v-model="productForm.detail" type="textarea" :rows="2" placeholder="请输入产品详细描述"></el-input>
            </el-form-item>            
            <el-form-item label="产品图片">
                <div class="box-card">
                    <img class="the_img" :src="productForm.cover" alt="">
                    <div class="btn-box">
                        <input type="file" accept="image/*" style="display: none" id="iptRef" @change="onFileChange" />
                        <el-button type="primary" icon="el-icon-plus" @click="chooseImg">选择图片</el-button> 
                    </div>
                </div>
             </el-form-item> 
            <el-button style="margin-left:48%" type="primary" @click="submitForm()">添加产品</el-button>
        </el-form>
    </div>
</template>

<script setup>
import {ref,reactive} from 'vue'
import {ElMessage} from 'element-plus'
import {addProductAPI} from '@/api'
import {useRouter} from "vue-router"

const router=useRouter()
const productFormRef=ref()
const productForm=reactive({
    title:'',
    author:'',
    introduction:'',
    detail:'',
    cover:''
})

const productFormRules=reactive({
    title: [{ required: true, message: '请输入产品名称', trigger: 'blur' }],
    author: [{ required: true, message: '请输入设计工程师', trigger: 'blur' }],
    introduction: [{ required: true, message: '请输入产品简要简介', trigger: 'blur' }],
    detail: [{ required: true, message: '请输入产品详细描述', trigger: 'blur' }]
})

// 添加用户
const submitForm=()=>{
    productFormRef.value.validate(async valid=>{
        if(valid){
            const {data:res}=await addProductAPI(productForm)
            if(res.status!==0){
                ElMessage.error('添加失败')
            }else{
                ElMessage.success('添加成功') 
                router.push('/product-manage/productlist')
            }
        }
    })
}

// 图片
const chooseImg=function () {
    // 模拟点击行为
    document.getElementById('iptRef').click()
  }
const onFileChange=function (e) {
    const files = e.target.files // 拿到用户选择的文件数组
    if (files.length === 0) {
     
    } else {
      // 解决方案2：文件->base64字符串（此字符串是可以发给后台的）
      const fr = new FileReader()
      fr.readAsDataURL(files[0]) // 传入文件对象开始读
      fr.onload = (e) => { 
        productForm.cover = e.target.result // 赋予给变量，让他显示在img的src里
      }
    }
  }
</script>

<style lang="scss" scoped>
.demo-ruleForm{
    margin-top: 50px;
}
// 图片
.btn-box {
  margin-top: 10px;
}
.preview {
  object-fit: cover;
}
.the_img{
    width: 150px;
    height: 150px;
}
</style>